package com.example.lol;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.example.lol.ui.FifthBottomFragment;
import com.example.lol.ui.FirstBottomFragment;
import com.example.lol.ui.SecondBottomFragment;
import com.example.lol.ui.adapter.BottomAdapter;
import com.example.lol.ui.adapter.HomeAdapter;
import com.example.lol.ui.adapter.NewsAdapter;
import com.example.lol.ui.bean.NewBean;
import com.example.lol.ui.bean.TopCategoryBean;
import com.example.lol.R;
import com.example.lol.ui.adapter.Adapter;
import com.example.lol.ui.bean.TopCategoryBean;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public  class lolActivity extends AppCompatActivity {
    private RecyclerView listView;
    private Adapter mNewsAdapter;
    private ViewPager mViewPager;
    private BottomNavigationView bottomNav;
    private TabLayout tabLayout;
    private HomeAdapter mVpAdapter;
    private  ImageView left_button;
    private DrawerLayout dl;
    private FrameLayout contentView;
    private TextView setBtn;
    private LinearLayout top;

    private final SparseArray<Fragment> mFragmentCache = new SparseArray<>();

    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dl = findViewById(R.id.drawer_layout);
        left_button = findViewById(R.id.left_button);
        left_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dl.openDrawer(GravityCompat.START);
            }
        });
        //左边
        listView = findViewById(R.id.recycle);
        listView.setLayoutManager(new LinearLayoutManager(this));
        mNewsAdapter = new Adapter();
        listView.setAdapter(mNewsAdapter);
        TopCategoryBean[] userBeans = new TopCategoryBean[7];
        for (int i = 0; i < 6; i++) {
            userBeans[i] = new TopCategoryBean();
        }
        userBeans[0].setUserName("我的评论");
        userBeans[1].setUserName("与我相关");
        userBeans[2].setUserName("我的订单");
        userBeans[3].setUserName("我的任务");
        userBeans[4].setUserName("我的收藏");
        userBeans[5].setUserName("活动中心");
        userBeans[0].setHeadId(R.drawable.one);
        userBeans[1].setHeadId(R.drawable.two);
        userBeans[2].setHeadId(R.drawable.three);
        userBeans[3].setHeadId(R.drawable.four);
        userBeans[4].setHeadId(R.drawable.five);
        userBeans[5].setHeadId(R.drawable.six);
        for (int i = 0; i <= 5; i++) {
            mNewsAdapter.addData(userBeans[i]);
        }
        mNewsAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                TopCategoryBean bean = mNewsAdapter.getItem(position);
                Toast.makeText(lolActivity.this, "" + userBeans[position].getUserName(), Toast.LENGTH_SHORT).show();
            }
        });
        setBtn =findViewById(R.id.tv_setting_btn);
        setBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(lolActivity.this, "设置", Toast.LENGTH_SHORT).show();
                Intent intent =new Intent();
                intent.setClass(lolActivity.this,SettingActivity.class);
                startActivity(intent);
            }
        });


        //第一页的TableLayout
//        mViewPager = findViewById(R.id.vp_content_1);
//       tabLayout = findViewById(R.id.bottom_demo);
//        mVpAdapter = new HomeAdapter(getSupportFragmentManager());
//        mViewPager.setAdapter(mVpAdapter);
//        tabLayout.setupWithViewPager(mViewPager);
        //

        top = findViewById(R.id.top1);
        contentView = findViewById(R.id.frameLayout_bottom);
        bottomNav = findViewById(R.id.bottom_nav_view);
        bottomNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                Fragment fragment = getContent(item);

                if  (item.getItemId()!=R.id.navigation_4) {
                    top.setVisibility(View.VISIBLE);
                }
                else if(item.getItemId()==R.id.navigation_4){
                    top.setVisibility(View.GONE);
                }

//                getSupportFragmentManager().beginTransaction().commit();

                getSupportFragmentManager()
                        .beginTransaction()
                        .replace(R.id.frameLayout_bottom, fragment)
//                        .show(fragment)
                        .commit();

//                getSupportFragmentManager().beginTransaction().replace(R.id.frameLayout_bottom,fragment).commit();


//                    .replace(R.id.frameLayout_bottom,new NewsFragment()).commit();
                return true;
            }
        });
        bottomNav.setSelectedItemId(R.id.navigation_0);


    }
    private Fragment getContent(MenuItem menuItem) {
        int id = menuItem.getItemId();
        Fragment fragment = mFragmentCache.get(id);
        if (fragment == null) {
            fragment = nf(menuItem);
            mFragmentCache.put(id, fragment);
        }
        return fragment;
    }

    private Fragment nf(MenuItem menuItem) {
        Fragment fragment;
        switch (menuItem.getItemId()){
//            case R.id.navigation_0:
//                fragment =new FirstBottomFragment();
//                break;
            case R.id.navigation_1:
                fragment = new SecondBottomFragment();
                break;
            case R.id.navigation_4:
                fragment = new FifthBottomFragment();
                break;
//            case R.id.navigation_3:
//                fragment = new FourthBottomFragment();
//                break;
            default:
                fragment =new FirstBottomFragment();
        }

//        fragment.setArguments(FirstBottomFragment.newArgument(menuItem.getTitle().toString()));
        return fragment;
    }
}
